package com.qianfeng.smartplatform.controller;


//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼            BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  


import com.github.pagehelper.PageInfo;
import com.qianfeng.smartplatform.dto.R;
import com.qianfeng.smartplatform.pojo.Device;
import com.qianfeng.smartplatform.service.DeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * Created by jackiechan on 2021/12/23 15:00
 *
 * @author jackiechan
 */
@RestController
@RequestMapping("/devices")
@Api(tags = "设备管理模块" )
public class DeviceController {
    @Autowired
    private DeviceService deviceService;

    @PostMapping("/device")
    @ApiOperation("添加设备")
    public R AddDevice(@RequestBody Device device) {
        deviceService.AddDevice(device);
        return R.setOK(null);
    }

    @PutMapping("/device")
    @ApiOperation("更新设备")
    public R updateDevice(@RequestBody Device device) {
        deviceService.updateDevice(device);
        return R.setOK(null);
    }

    @GetMapping("/device/{id}")
    @ApiOperation("根据id查询设备")
    public R getDeviceById(@ApiParam(name = "id",value = "主键",defaultValue = "-1") @PathVariable String id) {
        Device device = deviceService.getDeviceById(id);
        return R.setOK(device);
    }

    @GetMapping("/devices")
    @ApiOperation("分页搜索设备")
    public R getAllDevices(@ApiParam(name = "page",value = "第几页",defaultValue = "1") @RequestParam(defaultValue = "1",required = false) int page,@ApiParam(name = "limit",value = "每页数量",defaultValue = "10")  @RequestParam(required = false,defaultValue = "10") int limit, @ApiParam(name = "sceneId",value = "场景id")  @RequestParam(required = false)  Long sceneId) {
        PageInfo<Device> pageInfo = deviceService.getAllDevices(page, limit,sceneId);
        return R.setOK(pageInfo);
    }

    @DeleteMapping("/devices")
    @ApiOperation("删除设备")
    public R deleteDevices(@ApiParam(name = "ids",required = true,value = "主键")@RequestParam String[] ids) {
        deviceService.deleteDevices(Arrays.asList(ids));
        return R.setOK(null);
    }

    @PostMapping("/command/{uuid}/{command}")
    @ApiOperation("向设备发送指令")
    public R sendControl(@ApiParam(name = "uuid",value = "设备uuid",required = true) @PathVariable String uuid,@ApiParam(name = "command",value = "发送的指令",required = true)  @PathVariable String command) {
        deviceService.sendControl(uuid, command);
        return R.setOK("指令发送中");
    }
}
